﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DAL;
using System.Web.Services;
using DevExpress.Web.ASPxEditors;

public partial class BasicInfo_ProTypeInfor : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "2007"; }
    }


    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? (CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false) : false;
                }
            }
            else
            {
                return false;
            }
        }
    }



    void InitColumsText(GridViewColumn column, string value)
    {
        GridViewDataTextColumn gvText = column as GridViewDataTextColumn;
        gvText.PropertiesTextEdit.NullText = value;
        gvText.ReadOnly = true;
    }
    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        string ppbm = e.NewValues[0].ToString();     //部门编码
        //检查输入的部门编码是否重复
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            var result = lqdb.css_ppdj.FirstOrDefault(r => r.ppbm == ppbm);
            if (result != null)
            {
                throw new Exception("品牌编码已经存在！");
            }

        }
        string strDate = DateTime.Now.ToShortDateString();
        e.NewValues["founder"] = CMSUser.Current.ID;
        e.NewValues["fdate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }

    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    protected void gridView_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
    {

        if (e.Column.FieldName == "ppbm")
        {
            if (e.Value == null)
            {
                using (DAL.DataClassesDataContext db = new DataClassesDataContext())
                {
                    int number = db.css_ppdj.Count() + 1;
                    (e.Editor as ASPxTextBox).Text = WebUtility.GenerateCode(number.ToString(), 'P', 5);
                }
            }
        }
        if (e.Column.FieldName == "ppmc")
        {
            e.Editor.Focus();
        }
        e.Editor.SetClientSideEventHandler("KeyPress", @"function(s,e){var key = event.keyCode;if( key==13){EnterAsTab();}}");
    }
    //ajax客户端获取数据
    [WebMethod]
    public static string[] GetUsers(string ppbm)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var us = context.css_ppdj_rybm.Where(u => u.ppbm.Trim() == ppbm.Trim()).Select(u => u.rybm).ToArray();
            return us;
        }
    }
    //保存品牌负责人
    protected void gvUserList_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.Parameters))
        {
            List<string> IDlist = gvUserList.GetSelectedFieldValues("rybm").Select<object, string>(o => o.ToString()).ToList();
            using (DataClassesDataContext lqdb = new DataClassesDataContext())
            {
                foreach (var item in IDlist)
                {
                    css_ppdj_rybm de = lqdb.css_ppdj_rybm.FirstOrDefault(o => o.ppbm == e.Parameters && o.rybm == item.Trim());
                    if (de == null)
                    {
                        de = new css_ppdj_rybm();
                        de.ppbm = e.Parameters;
                        de.rybm = item.Trim();
                        lqdb.css_ppdj_rybm.InsertOnSubmit(de);
                    }
                }
                lqdb.SubmitChanges();
            }
        }
    }
}